package clinic.ms.controller;

import clinic.ms.entity.Admin;
import clinic.ms.service.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.List;
@Controller
public class AdminController {

    @Autowired
    AdminServiceImpl adminService=new AdminServiceImpl();

    // 添加管理员-get
    @GetMapping("/addUser")
    public String getAddUser(){
        return "/addUser";
    }

    //添加管理员-post
    @PostMapping("/adduser")
    public ModelAndView adduser(@RequestParam(value = "userName",required = false) String userName,
                                 @RequestParam(value = "password",required = false) String password,
                                 @RequestParam(value = "realName",required = false)String realName,
                                 @RequestParam(value = "userSex",required = false)char userSex,
                                 @RequestParam(value = "userPhone",required = false)String userPhone,
                                 @RequestParam(value = "userRemarks",required = false)String userRemarks)
    {
        ModelAndView modelAndView = new ModelAndView("/addUser");
        Admin admin = new Admin(userName,password,realName,userSex,userPhone,userRemarks);
        String addResult = adminService.insertAdmin(admin);
        if(addResult == null)  // 插入失败
            modelAndView.addObject("result", "插入失败，请重试!");
        else   // 插入成功
            modelAndView.addObject("result", "插入成功，用户名："+addResult);
        return modelAndView;
    }

    // 显示全部信息
    @GetMapping("/UserAdmin")
    public ModelAndView listDrug(){
        ModelAndView modelAndView = new ModelAndView("/UserAdmin");
        List<Admin> admins;
        // 查询所有
        admins = adminService.queryAdByNames("");
        modelAndView.addObject("admins",admins);
        return modelAndView;
    }

    //跳转到编辑
    @GetMapping("/editUser")
    public ModelAndView editUser(@RequestParam("id") String userName)
    {
        Admin admin = adminService.queryAdByName(userName);
        ModelAndView modelAndView = new ModelAndView("/editUser");
        modelAndView.addObject("admin",admin);
        //将查询到的patient信息放进modelAndView里
        return modelAndView;
    }

    @PostMapping("/editUser")
    public ModelAndView editUser(@RequestParam(value = "userName",required = false) String userName,
                                 @RequestParam(value = "password",required = false) String password,
                                 @RequestParam(value = "realName",required = false)String realName,
                                 @RequestParam(value = "userSex",required = false)char userSex,
                                 @RequestParam(value = "userPhone",required = false)String userPhone,
                                 @RequestParam(value = "userRemarks",required = false)String userRemarks)
    {
        ModelAndView modelAndView = new ModelAndView("/editUser");
        Admin admin = new Admin(userName,password,realName,userSex,userPhone,userRemarks);
        if(adminService.updateAdByName(admin))
            modelAndView.addObject("admin",adminService.queryAdByName(userName));
        else modelAndView.addObject("admin",admin);
        return modelAndView;
    }


    // 删除
    @PostMapping("/deleteUser")
    public ModelAndView deleteUser(String id) {

        try {
            adminService.deleteAdByName(id);
        } catch (Exception e) {
            System.out.println(e);
        }
        return new ModelAndView(new RedirectView("/UserAdmin"));
    }

    //查询
    @PostMapping("/searchUser")
    public ModelAndView searchUser(String name){
        List<Admin> admins;
        //System.out.println("++++"+no+name);
        admins = adminService.queryAdByNames(name);
        ModelAndView modelAndView = new ModelAndView("/UserAdmin");
        modelAndView.addObject("admins",admins);
        return modelAndView;
    }

}